package com.zou.music_downloader.secondaryfragments;

import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.zou.music_downloader.R;
import com.zou.music_downloader.SearchFragment_;
import com.zou.music_downloader.utils.SharedPreferencesUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zou on 2016/3/11.
 */
@EFragment(R.layout.sfragment_hotword)
public class HotwordFragment extends Fragment {

    List datas;

    @ViewById(R.id.lv_history)
    ListView lv;

    @ViewById(R.id.tv_nohistory)
    TextView textView;

    @ViewById(R.id.delete_history)
    Button button;

    private SimpleAdapter simpleAdapter;

    @AfterViews
    void afterView() {
        datas = new ArrayList();

        simpleAdapter = new SimpleAdapter(getContext(), datas, R.layout.item_simple
                , new String[]{"key"}, new int[]{R.id.text_simple});

        lv.setAdapter(simpleAdapter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferencesUtils.setParam(getContext(), "history", "");
                getSp();
            }
        });


        getSp();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SearchFragment_.searchview.setQuery((CharSequence) ((Map) datas.get(position)).get("key"), false);
            }
        });
    }

    public void getSp() {
        datas.clear();
        String searchedHistoryString = (String) SharedPreferencesUtils.getParam(getContext(), "history", "");
        Log.e("wocao", "getSp: " + searchedHistoryString);
        String[] strings = {};
        if (!TextUtils.isEmpty(searchedHistoryString)) {
            strings = searchedHistoryString.split("\\|+");
        }
        for (String string :
                strings) {
            if (!TextUtils.isEmpty(string)) {
                Map temp = new HashMap();
                temp.put("key", string);
                datas.add(temp);
            }
        }

        simpleAdapter.notifyDataSetChanged();

        if (datas.size() == 0) {
            textView.setVisibility(View.VISIBLE);
            button.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.GONE);
            button.setVisibility(View.VISIBLE);
        }
        Log.e("wocao", "getSp: " + searchedHistoryString);
    }

}
